package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;

import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;


/**
 * 属性分组
 *
 * @author jiangqi
 * @email 70016952@qq.com
 * @date 2020-07-29 10:55:14
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    /*
    获取分类下所有分组&关联属性
    /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithattrByCatelogId(@PathVariable("catelogId") String catelogId) {
       List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithattrByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    //attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R delete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrService.delete(attrGroupRelationVos);

        return R.ok();
    }

    //attr/relation
   /*
   分组关联属性
   */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.relation(vos);

        return R.ok();
    }


    //http://localhost:88/api/product/attrgroup/1/attr/relation
    /*
    根据属性分组id 查询属性列表
     */
    @GetMapping("/{groupId}/attr/relation")
    public R attrRelation(@PathVariable("groupId") String groupId) {

        List<AttrEntity> attrEntities = attrService.getRelationAttr(groupId);

        return R.ok().put("data", attrEntities);
    }

    // /1/noattr/relation
    @GetMapping("/{groupId}/noattr/relation")
    public R noaAttrRelation(@PathVariable("groupId") String groupId, @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.getNoaAttrRelation(groupId, params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.catelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
